/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2010 Alexandre Croiseaux
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package project_name.uis.content
{
	import flash.events.MouseEvent;
	import marcel.core.Plugin;
	import marcel.display.gui.buttons.SimpleTextButton;
	import marcel.display.screen.FullScreenManager;
	import marcel.events.VideoEvent;
	import marcel.media.video.VideoDisplay;
	import marcel.media.video.VideoSubtitles;
	import project_name.uis.BasicUI;
	
	
	
	public class VideoUI extends BasicUI
	{
		private var video:VideoDisplay;
		private var fsm:FullScreenManager;
		
		override public function close():void
		{
			super.close();
			video.stop();
		}
		
		override public function initialize():void
		{
			video = new VideoDisplay();
			video.x = 50;
			video.y = 100;
			video.setVolume(.5);
			video.setInfosVisible(true);
			video.setVideoSize(320, 240);
			video.setVideoURL(Plugin.plugin.config.getVar("videodir") + "test.flv");
			
			// subtitles
			if (Plugin.plugin.assetManager.hasAsset("subtitles"))
			{
				var subtitles:VideoSubtitles = new VideoSubtitles();
				subtitles.setSRTContent(Plugin.plugin.assetManager.getTextAsset("subtitles"));
				video.setSubtitles(subtitles);
			}
			
			// available events
			video.addEventListener(VideoEvent.START, onVideoEvent);
			video.addEventListener(VideoEvent.END, onVideoEvent);
			video.addEventListener(VideoEvent.LOOP, onVideoEvent);
			video.addEventListener(VideoEvent.STOP, onVideoEvent);
			video.addEventListener(VideoEvent.RESIZE, onVideoEvent);
			//video.addEventListener(VideoEvent.BUFFERING, onVideoEvent);
			//video.addEventListener(VideoEvent.PLAYING, onVideoEvent);
			video.addEventListener(VideoEvent.ERROR, onVideoEvent);
			video.addEventListener(VideoEvent.METADATA, onVideoEvent);
			video.addEventListener(VideoEvent.LOAD_COMPLETE, onVideoEvent);
			video.addEventListener(VideoEvent.STATUS, onVideoEvent);
			video.addEventListener(VideoEvent.CUE_POINT, onVideoEvent);
			
			addChild(video);
			
			fsm = new FullScreenManager(stage);
			
			var tb:SimpleTextButton = new SimpleTextButton("play / pause");
			tb.x = 400;
			tb.y = 100;
			tb.addEventListener(MouseEvent.CLICK, onTogglePause);
			addChild(tb);
			
			tb = new SimpleTextButton("Fullscreen");
			tb.x = 500;
			tb.y = 100;
			tb.addEventListener(MouseEvent.CLICK, onFS);
			addChild(tb);
			
			tb = new SimpleTextButton("Show/Hide Video Infos");
			tb.x = 400;
			tb.y = 130;
			tb.addEventListener(MouseEvent.CLICK, onShowInfos);
			addChild(tb);
		}
		
		private function onFS(e:MouseEvent):void {
			fsm.setFullScreen(true, video.getRect(this));
		}
		
		// Events
		public function onVideoEvent(evt:VideoEvent):void {
			//debug(evt.type + " " + evt.value);
		}
		
		public function onTogglePause(evt:MouseEvent):void {
			video.isPlaying() ? video.pause() : video.play();
		}
		
		public function onShowInfos(evt:MouseEvent):void {
			video.setInfosVisible(!video.isInfosVisible());
		}
		
	}

}